<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02-定时器的应用(一)</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #box1 {
            width: 100px;
            height: 100px;
            background-color: #bfa;
            position: absolute;
            left: 0;
        }
    </style>


    <script>

        window.onload = function () {

            var box1 = document.getElementById("box1");
            var btn01 = document.getElementById("btn01");

            var speed = 10;

            var timer;

            btn01.onclick = function () {

                clearInterval(timer);

                timer = setInterval(function () {

                    var oldValue = parseInt(getStyle(box1, "left"));
                    var newValue = oldValue + 10;

                    if (newValue > 800) {
                        newValue = 800;
                    }

                    box1.style.left = newValue + "px";

                    if (newValue == 800) {
                        clearInterval(timer);
                    }

                }, 30);

            }
        }

        function getStyle(obj, name) {
            if (window.getComputedStyle) {
                return getComputedStyle(obj, null)[name];
            } else {
                return obj.currentStyle[name];
            }
        }


    </script>
</head>

<body>

    <button id="btn01">点击按钮以后box1向右移动</button>

    <br /><br />

    <div id="box1"></div>

    <div style="width: 0; height: 1000px; border-left: 1px solid black; position: absolute; left: 800px; top: 0;"></div>

</body>

</html>